//
//  ViewController.m
//  03-UIGestureRecognizer
//
//  Created by dllo on 16/7/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, assign) int i;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
#pragma mark - 手势识别器分为7种
    // 1 轻拍 tap
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
    // 点击几次触发方法, 默认为1
    tap.numberOfTapsRequired = 2;
    // 几个手指触发
    tap.numberOfTouchesRequired = 2;
    
    // 2 长按 longPress
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 长按几秒后开始, 默认为0.5
    longPress.minimumPressDuration = 1;
    // 默认10像素, 长按失败前, 最大的移动距离
    longPress.allowableMovement = 10;
    [self.view addGestureRecognizer:longPress];
    
    // 3 轻扫 Swipe
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAcion:)];
    // direction可以设置多个 前提是多个方向所触发的行为要相同
    // 如果多个枚举方向所触发的行为不同, 需要创建多个轻扫的手势
    // 左右与上下有冲突
    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
    
    // 练习
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 200)];
    _imageView.image = [UIImage imageNamed:@"0.jpg"];
    _imageView.userInteractionEnabled = YES;
    [self.view addSubview:_imageView];

//    UISwipeGestureRecognizer *swipeChangedLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeChangeImageLeft:)];
//    swipeChangedLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//    [_imageView addGestureRecognizer:swipeChangedLeft];
//    
//    UISwipeGestureRecognizer *swipeChangeImageRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeChangeImageRight:)];
//#pragma mark - 按位枚举 
//    // 4个选项 -> 1 1 1 1
//    swipeChangeImageRight.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp;
//    [_imageView addGestureRecognizer:swipeChangeImageRight];
//    self.i = 0;
    
    // 4 平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [_imageView addGestureRecognizer:pan];
    // 5 捏合
    UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [_imageView addGestureRecognizer:pin];
    
    // 6 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [_imageView addGestureRecognizer:rotation];
    
    // 7 屏幕边缘手势
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePan:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"start");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
}

- (void)swipeAcion:(UITapGestureRecognizer *)swipe {
    NSLog(@"轻扫");
}

- (void)swipeChangeImageLeft:(UISwipeGestureRecognizer *)swipe {
    UIImageView *imageView = (UIImageView *)swipe.view;
    self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", ++_i]];
}

- (void)swipeChangeImageRight:(UISwipeGestureRecognizer *)swipe {
    UIImageView *imageView = (UIImageView *)swipe.view;
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", --_i]];
}

- (void)panAction:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateBegan) { // 平移开始
        
    } else if (pan.state == UIGestureRecognizerStateChanged) { // 平移中
        // 平移手势 根据其他View上移动的过程 得到最终点
        CGPoint point = [pan translationInView:pan.view];
        // 根据point使pan.view移动
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        // pan移动的点置空
        [pan setTranslation:CGPointZero inView:pan.view];
    } else if (pan.state == UIGestureRecognizerStateEnded) { // 平移结束
        pan.view.transform = CGAffineTransformIdentity;
    }
}

- (void)pinchAction:(UIPinchGestureRecognizer *)pin {
    pin.view.transform = CGAffineTransformScale(pin.view.transform, pin.scale, pin.scale);
    // 设置缩放倍数为1
    pin.scale = 1;
}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {
    // 转换中, 根据上次转换继续改变
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    // 旋转的弧度置为0
    rotation.rotation = 0;
}

- (void)screenEdgePan:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {
    NSLog(@"屏幕边缘手势");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
